<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="证件号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入证件号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急联系人名字" prop="guardianName">
        <el-input
          v-model="queryParams.guardianName"
          placeholder="请输入紧急联系人名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急联系人手机" prop="guardianPhone">
        <el-input
          v-model="queryParams.guardianPhone"
          placeholder="请输入紧急联系人手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:management:add']"
        >绑定
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户账号" align="center" prop="account"/>
      <el-table-column label="用户昵称" align="center" prop="nickName"/>
      <el-table-column label="用户邮箱" align="center" prop="email"/>
      <el-table-column label="手机号码" align="center" prop="phoneNumber"/>
      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="头像url" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-avatar
            shape="square"
            size="small"
            :src="scope.row.avatar"
          />
        </template>
      </el-table-column>
      <el-table-column label="身份证类型" align="center" prop="idType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.id_type" :value="scope.row.idType"/>
        </template>
      </el-table-column>
      <el-table-column label="证件号" align="center" prop="idNumber"/>
      <el-table-column label="紧急联系人名字" align="center" prop="guardianName"/>
      <el-table-column label="紧急联系人手机" align="center" prop="guardianPhone"/>
      <el-table-column label="家庭住址" align="center" prop="address"/>
      <el-table-column label="真实姓名" align="center" prop="realName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAdd(scope.row)"
            v-hasPermi="['customer:management:customerBind']"
          >绑定
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {
  selectUnBindCustomerList,
  delManagement,
  addManagement,
  updateManagement
} from '@/api/customer/management'
import { addCustomerBind } from '@/api/customer/customerBind'

export default {
  name: 'CustomerSelect',
  props: {
    customerId: {
      type: String,
      default: ''
    },
    popupVisible: {
      type: Boolean,
      default: false
    }
  },
  dicts: ['sys_user_sex', 'sys_normal_disable', 'id_type', 'delete_flag', 'relations_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      managementList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      accountOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        account: null,
        nickName: null,
        email: null,
        phoneNumber: null,
        sex: null,
        avatar: null,
        status: null,
        idType: null,
        idNumber: null,
        guardianName: null,
        guardianPhone: null,
        address: null,
        realName: null
      },
      // 表单参数
      form: {},
      relationsType: null,
      // 表单校验
      rules: {
        account: [
          { required: true, message: '用户账号不能为空', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '用户昵称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.queryParams.notCustomerId = this.customerId
    this.form.customerId = this.customerId
    this.getList()
  },
  methods: {
    /** 查询客户管理列表 */
    getList() {
      this.loading = true
      selectUnBindCustomerList(this.queryParams).then(response => {
        this.managementList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        account: null,
        nickName: null,
        email: null,
        phoneNumber: null,
        sex: null,
        avatar: null,
        status: null,
        remark: null,
        idType: null,
        idNumber: null,
        guardianName: null,
        guardianPhone: null,
        address: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        realName: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd(row) {
      const h = this.$createElement
      var _this = this
      this.$msgbox({
        title: '请选择关系', //弹框标题
        //弹框信息
        message: h('el-select', {
            props: {
              value: '',
              filterable: true
            },
            ref: 'selectView',
            on: {
              change: e => {
                _this.relationsType = e
                _this.$refs.selectView.value = e
              }
            }
          },
          [
            _this.dict.type.relations_type.map(it => {
              return h('el-option', {
                props: {
                  key: it.value,
                  label: it.label,
                  value: it.value
                }
              })
            })
          ]
        ),
        showCancelButton: true,
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(_ => {
        //成功操作
        if(!_this.relationsType){
          this.$modal.msgSuccess('请先选择绑定关系类型')
          return
        }
        _this.handleAdd2(row)
        _this.relationsType=null
      }).catch(_ => {
        //取消操作
        console.log('取消')
        _this.relationsType=null
      })
    },
    /** 新增按钮操作 */
    handleAdd2(row) {
      let ids = row.id || this.ids
      if (row.id) {
        ids = row.id.split(',')
      } else {
        ids = this.ids
      }
      if (!ids) {
        this.$modal.msgSuccess('请先勾选需要绑定的客户')
        return
      }
      const params = {}
      params.customerId = this.form.customerId
      params.bindCustomerIds = ids
      params.relationsType=this.relationsType
      addCustomerBind(params).then(response => {
        this.$modal.msgSuccess('修改成功')
        this.handleQuery()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除客户管理编号为"' + ids + '"的数据项？').then(function() {
        return delManagement(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customer/management/export', {
        ...this.queryParams
      }, `management_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
